/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.nio.ByteBuffer;
import java.util.Vector;

/**
 * This action is used to communicate changes in a cars condition
 * a cars condition can be set to on fire, movable, damaged, and
 * their lights have different settings
 * @author zamille
 */
public class CarConditionAction extends Action
{
    /** is the car on fire*/
    boolean onFire;
    /** is the car movable*/
    boolean movable;
    /** is the car damaged*/
    boolean damaged;
    /** what are the cars lights set to, i.e. on,off,etc(for IDOT only)
     * all possible lihgtSetting values can be found in ServerConstants*/
    int lightSetting;


    /**
     *
     * @param car
     * @param on fire
     * @param can move
     * @param is damaged
     * @param cars lights: possible values found in ServerConstants
     */
    public CarConditionAction(Automobile car, boolean fire, boolean moves, boolean damage, int lights)
    {
        super(car);
        onFire = fire;
        movable = moves;
        damaged = damage;
        lightSetting = lights;
        this.messageType = Message.CAR_CONDITION_MESSAGE;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public CarConditionAction(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes,items);
        this.messageType = Message.CAR_CONDITION_MESSAGE;
    }

    /**
     * Builds an Action from a save string, presumably created by an action when saving to a file
     * this action should be identical to that one
     * @param saveString
     * @param items: Master Item List used to build the action
     */
    public CarConditionAction(String substring, Vector<Item> itemList) {
        fromSaveString(substring, itemList);
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this methods completes
     */
    @Override
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        try {
            bytes = super.fromBytes(bytes, items);   //it uses the inherited method to build
            onFire = ByteChanger.getBoolean(bytes, 0);  //the inherited members from the ByteBuffer
            movable = ByteChanger.getBoolean(bytes, 1);
            damaged = ByteChanger.getBoolean(bytes, 2);
            lightSetting = bytes.getInt(3);

             bytes = ByteChanger.truncate(bytes, 7);

            return bytes;
        } catch (Exception ex) {

        }
        return bytes;

    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = super.toByte();   //it uses the inherited method to put
        buf.put(ByteChanger.booleanToByte(onFire));  //the inherited members in the ByteBuffer
        buf.put(ByteChanger.booleanToByte(movable));
        buf.put(ByteChanger.booleanToByte(damaged));
        buf.putInt(lightSetting);

        return buf;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    public String toSaveString()
    {
        String text = super.toSaveString();   //it uses the inherited method to put
        text+= onFire + "\t";        //the inherited members in the Save String
        text+= movable + "\t";
        text+= damaged + "\t";
        text+= lightSetting + "\t";
        return text;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    @Override
    protected String fromSaveString(String text, Vector<Item> items)
    {
        text = super.fromSaveString(text, items);   //it uses the inherited method to build
        String substring = ByteChanger.getNextValue(text);  //the inherited members from the Save String
        onFire = Boolean.parseBoolean(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        movable = Boolean.parseBoolean(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        damaged = Boolean.parseBoolean(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        lightSetting = Integer.parseInt(substring);

        text = ByteChanger.incrementString(text);
        return text;
    }
}
